package com.xiaoyao.novel.crawl;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.parser.MotanParser;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Chapter;
import com.xiaoyao.novel.util.ClassUtil;
import com.xiaoyao.novel.util.HttpUtil;

/**
 * 很容易被封了IP
 * @author Administrator
 *
 */
public class MotanCrawl extends BaseCrawl {
	private static HttpUtil httpBook = new HttpUtil("GBK");
	private static Logger logger = Logger.getLogger(QidianCrawl.class);

	public MotanCrawl() {
		tag = Book.TYPE_MT62;
	}

	/**
	 * 爬一本书,目录
	 * 
	 * @param bookId
	 */
	public void crawlBook(Book book) {
		String page = "";
		try {
			//http://www.62mt.com/files/article/html/0/2/index.html
			String categoryUrl = "http://www.62mt.com/files/article/html/" + book.webId + "/index.html";
			book.bookUrl= categoryUrl;
			if (ClassUtil.isEmpty(book.authorName)) {
				page = httpBook.getPage(categoryUrl);
				logger.info("读取简介:" + categoryUrl);
				logger.debug(page);
				MotanParser.parseDescrib(page, book);
			}
 
			List<Chapter> list = MotanParser.parseBook(page, book);
			logger.info("章节数量:" + list.size());
			for (int i = book.savedCount; i < list.size(); i++) {
				Chapter chapter = book.list.get(i);
				crawlChapter(chapter);
			}

		} catch (IOException e) {
			logger.error(e);
		}
	}

	/**
	 * 爬章节类容
	 * 
	 * @param chapter
	 * @throws IOException
	 */
	public void crawlChapter(Chapter chapter) throws IOException {
		String urlStr = chapter.chapterUrl;
		logger.info("爬取章节 :[" + urlStr + "]");
		String page = httpBook.getPage(urlStr);
		logger.debug(page);
		MotanParser.parseChapter(page, chapter);
		chapter.chapterContent = filter(chapter.chapterContent);
		logger.info("内容长度:" + chapter.chapterContent.length());
	}

	// ///////////////////////////////////////////
	// ///////////////////////////////////////////
	/**
	 * 过滤
	 * 
	 * @param page
	 * @return
	 */
	public String filter(String page) {
		page = page.replaceAll("http://http://shuhuang.la", "");
		page = page.replaceAll("&nbsp;", " ");
		page = page.replaceAll("书荒", "");
		// 其他替换
		return page;
	}

	public static void main(String[] args) {
		Book book = new Book();
		book.webId = "0/2";
		MotanCrawl songchaoCrawl = new MotanCrawl();
		songchaoCrawl.crawlBook(book);
	}
}
